package com.ivan.stu.notetool.utils;

import android.content.Context;


import com.ivan.stu.notetool.View.LoadDialog;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.List;

/**
 * Created by IVAN on 2019/2/28.
 */

public class LookPicture implements HttpDownLoad.OnDownloadListener {

    private static  ImageDownLoadCallBack mCallBack;

    public interface ImageDownLoadCallBack{

        public void onCompleted(String localPath);
        public void onError(String error);
        public void onFileOpened(String localPath);
    }

    /**
     * 查看图片
     */


    //该方法仅限一次查看一张图片
    public static void look(String url, Context context,ImageDownLoadCallBack callBack) {
        mCallBack = callBack;

        if (url.startsWith("file://")) {
            String path = url.replaceAll("file://", "");
            File file = new File(path);
            if (file.exists()) {
                LocalOpen.openPicture(context, file.getAbsolutePath());
            }
        } else {

            String prefix = url.substring(url.lastIndexOf("."));
            String fileName = MD5.string2Md5(url) + prefix;
            File file = getFile(fileName);

            //首次点击图片查看图片时，需下载图片，储存在缓存文件中，再次点击时直接从缓存文件中获取

            if (file != null && file.exists()) {
                LocalOpen.openPicture(context, file.getAbsolutePath());

                        if (mCallBack!=null){

                            mCallBack.onCompleted(file.getAbsolutePath());
                        }

            } else {  //否则下载图片
                if (NetworkUtil.hasNetwork(context)) {
                    LookPicture lookPicture = new LookPicture(context);
                    lookPicture.download(url, file);
                }
            }
        }
    }


    //该方法用于依次预览多张图片

    public static void look(List<String> imageUrls,int position, Context context, ImageDownLoadCallBack callBack) {
        mCallBack = callBack;

        String url = imageUrls.get(position);

        if (url.startsWith("file://")) {
            String path = url.replaceAll("file://", "");
            File file = new File(path);
            if (file.exists()) {
                LocalOpen.openPicture(context, file.getAbsolutePath());
            }
        } else {

            String prefix = url.substring(url.lastIndexOf("."));
            String fileName = MD5.string2Md5(url) + prefix;
            File file = getFile(fileName);

            //首次点击图片查看图片时，需下载图片，储存在缓存文件中，再次点击时直接从缓存文件中获取

            LocalOpen.openPicture(context,imageUrls,position);

      /*      if (file != null && file.exists()) {
             //   LocalOpen.openPicture(context, file.getAbsolutePath());

                LocalOpen.openPicture(context,imageUrls,position);

                if (mCallBack!=null){

                    mCallBack.onCompleted(file.getAbsolutePath());
                }

            } else {  //否则下载图片
                if (NetworkUtil.hasNetwork(context)) {
                    LookPicture lookPicture = new LookPicture(context);
                    lookPicture.download(url, file);
                }
            }*/
        }
    }

    private static File getFile(String fileName) {

        File rootDir = FileManager.getRootDir();
        File cacheDir = FileManager.getDir(rootDir, DirType.DIR_PICTURE);
        return FileManager.getFile(cacheDir, fileName);
    }

    private WeakReference<Context> mContext;
    private File mLocalFile;
    private HttpDownLoad mHttpDownload;
    private LoadDialog mLoadDialog;

    public LookPicture(Context context) {
        mContext = new WeakReference<>(context);

        mHttpDownload = new HttpDownLoad();
        mHttpDownload.setOnDownloadListener(this);
        mLoadDialog = new LoadDialog.Builder(context).setMsg("正在下载原图...").setCancelable(false).build();

    }

    private void download(final String remoteUrl, final File localFile) {
        mLoadDialog.show();
        mLocalFile = localFile;
        Global.submit(new Runnable() {
            @Override
            public void run() {

                mHttpDownload.download(remoteUrl, localFile);

            }
        });
    }

    @Override
    public void onStart(long current, long total) {

    }

    @Override
    public void onDownloading(long current, long total) {
    }

    @Override
    public void onCompleted(long current, long total) {
        Global.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mLoadDialog.dismiss();
            }
        });
        if (mContext.get() != null) {

            Global.runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    //下载完成后就打开图片。
                    LocalOpen.openPicture(mContext.get(), mLocalFile.getAbsolutePath());

                    if (mCallBack!=null){

                        mCallBack.onCompleted(mLocalFile.getAbsolutePath());
                    }
                }
            });

        }
    }

    @Override
    public void onFail(final String msg) {
        Global.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mLoadDialog.dismiss();
                if (mCallBack!=null){

                    mCallBack.onError(msg);

                }
            }
        });
    }

}
